/**
 * @param {number[][]} boxTypes
 * @param {number} truckSize
 * @return {number}
 */
var maximumUnits = function(boxTypes, truckSize) {
  boxTypes.sort((a, b) => {
    return b[1] - a[1]
  });
  let count = 0;
  for(let i = 0; i < boxTypes.length; i++) {
    if(truckSize >= boxTypes[i][0]) {
      truckSize -= boxTypes[i][0];
      count += boxTypes[i][0] * boxTypes[i][1];
    } else {
      count += truckSize * boxTypes[i][1];
      truckSize = 0;
    }
    if(truckSize == 0) break;
  }
  return count;
};
let boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4;
console.log(maximumUnits(boxTypes, truckSize));
boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10;
console.log(maximumUnits(boxTypes, truckSize));
